package com.example.admin.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.admin.R;
import com.example.admin.SQLite.Complaint;
import com.example.admin.SQLite.LunTan;
import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.dao.LunTanDao;
import com.example.admin.activity.ForumDetailActivity;
import com.example.admin.activity.ForumDetailsActivity;
import com.example.admin.adapter.ForumAdapter;
import com.example.admin.adapter.ForumLunTanAdapter;

import java.util.ArrayList;

public class Tab3Fragment extends Fragment {
    RecyclerView recyclerView;
    ForumLunTanAdapter mAdapter;
LunTanDao lunTanDao;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab3, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lunTanDao = new LunTanDao(getActivity());
        mAdapter = new ForumLunTanAdapter();
        mAdapter.setBoo(true);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getActivity(), ForumDetailsActivity.class)
                        .putExtra("bean", mAdapter.getItem(position)));
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("是否执行此操作");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        lunTanDao.DeleteContacts(mAdapter.getItem(position).id);
                        mAdapter.remove(position);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
        ArrayList<LunTan> list = lunTanDao.getComPlans();
        mAdapter.setNewData(list);
    }
}